package cn.schoolwow.workflow.module.task.action.flow.complete.step;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.DeployNodeType;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.task.action.domain.CompleteTaskOption;
import cn.schoolwow.workflow.module.task.action.flow.common.SetNextNodeCommonFlow;
import cn.schoolwow.workflow.module.task.action.flow.common.SetNodeAssignerFinishedOptionCommonFlow;

public class QueryCompleteTaskNextNodeFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkCompleteTaskType(flowContext);
        setCurrentNode(flowContext);
        setNextNode(flowContext);
    }

    @Override
    public String name() {
        return "查询完成任务流程的下一个节点列表";
    }

    private void checkCompleteTaskType(FlowContext flowContext){
        CompleteTaskRequest completeTaskRequest = flowContext.checkInstanceData(CompleteTaskRequest.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        switch (completeTaskRequest.taskType){
            case REJECT:{
                //若为拒绝，则直接结束流程
                WorkFlowNode nextNode = dao.query(WorkFlowNode.class)
                        .addQuery("definitionId", workFlowInstance.getDefinitionId())
                        .addQuery("type", DeployNodeType.EndNode.value)
                        .execute()
                        .getOne();
                flowContext.putTemporaryData("nextNode", nextNode);
                flowContext.brokenCurrentFlow("完成任务类型为拒绝,结束流程");
            }break;
        }
    }

    private void setCurrentNode(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        WorkFlowTask workFlowTask = flowContext.checkInstanceData(WorkFlowTask.class);

        dao.addRecordString("根据工作流实例当前节点id查询工作流节点记录");
        WorkFlowNode currentWorkflowNode = dao.query(WorkFlowNode.class)
                .addQuery("id", workFlowTask.getNodeId())
                .execute()
                .getOne();
        flowContext.putTemporaryData("currentWorkflowNode", currentWorkflowNode);
    }

    private void setNextNode(FlowContext flowContext){
        WorkFlowNode currentWorkflowNode = flowContext.checkData("currentWorkflowNode", WorkFlowNode.class);

        flowContext.startFlow(new SetNodeAssignerFinishedOptionCommonFlow())
                .putCurrentCompositeFlowInstanceData(currentWorkflowNode)
                .execute();

        CompleteTaskOption completeTaskOption = flowContext.checkInstanceData(CompleteTaskOption.class);

        if(!completeTaskOption.setNextNode){
            flowContext.putTemporaryData("nextNode", currentWorkflowNode);
        }else{
            flowContext.executeFlowList(new SetNextNodeCommonFlow());
        }
    }

}